<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service\apply;


use app\admin\service\BaseService;
use app\channel\model\BoxGame;
use app\channel\model\BoxGmApply;
use app\channel\model\UserChannel;
use app\qsapi\model\Package;
use think\Db;

/**
 * 推广员分包管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/07/28
 * Class ApplyService
 * @package app\admin\service
 */
class BoxGmApplyService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxGmApply();
    }



    public function getGameList(): array
    {
        $userId = request()->getUserId() ?? 0;
        if (empty($userId)) return message('用户未登录', 0);
        $gameIds = UserChannel::where('id',$userId)->value('game_id');
        $gameList = BoxGame::field('id,game_name')->whereIn('id', $gameIds)->select()->toArray();
        return message('success', true, $gameList);
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since 2023/07/28
     * @author ZhangXuanNeng
     */
    public function getList(): array
    {
        $param = request()->param();
        // 查询条件
        $map = [];
        // 渠道名称
        $name = isset($param['game_id']) ? trim($param['game_id']) : '';
        if ($name) {
            $map[] = ['game_id', '=', $name];
        }
        // 有效标识
        $mark = isset($param['mark']) ? (int)$param['mark'] : 0;
        $this->arr = ['mark', '=', $mark];
        if ($mark) {
            $map[] = $this->arr;
        }

        $list = $this->model->where($map)->order('id desc')->paginate($param['limit'] ?? 10)->toArray();
        $promoteList = UserChannel::field('id,nickname')->select()->toArray();
        $promoteList = array_column($promoteList, 'nickname', 'id');
        $gameList = BoxGame::field('id,game_name')->select()->toArray();
        $gameList = array_column($gameList, 'game_name', 'id');
        $packageList = Package::field('id,package_name')->select()->toArray();
        $packageList = array_column($packageList, 'package_name', 'id');
        foreach ($list['data'] as &$item) {
            $item['promote_name'] = $promoteList[$item['promote_id']] ?? '';
            $item['game_name'] = $gameList[$item['game_id']] ?? '';
            $item['package_name'] = $packageList[$item['package_id']] ?? '';
        }
        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list['data'],
            "count" => $list['total'],
        );
    }

    public function edit()
    {
        $param = request()->param();
        $status = isset($param['status']) ? (int)$param['status'] : 0;
        $admin_id = request()->getUserId();

        //审核通过
        if (!empty($status)) {
            $param['enable_status'] = 2;
            $param['dispose_id'] = $admin_id;
            $param['dispose_time'] = time();
        }
        return parent::edit($param, false, true); // TODO: Change the autogenerated stub
    }


    /**
     * 设置状态
     * @return mixed
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function status()
    {
        $data = request()->param();
        if (!$data['id']) {
            return message('记录ID不能为空', false);
        }
        if (!isset($data['status'])) {
            return message('记录状态不能为空', false);
        }
        $error = '';
        $item = [
            'id' => $data['id'],
            'status' => $data['status'],
            'enable_status' => 2,
        ];
        $packageId = $this->model->where('id',$data['id'])->value('package_id');
        $packageInfo = Db::name('package')->where('id',$packageId)->find();
        if($packageInfo['sdk_version'] == 2 && !empty($packageInfo['appstore_url'])){
            $item['enable_status'] = 1;
        }
        if($packageInfo['sdk_version'] == 3){
            $item['enable_status'] = 1;
        }
        $rowId = $this->model->edit($item, $error);
        if (!$rowId) {
            return message($error, false);
        }
        return message();
    }

}